package wechat.rcv.controller;

import com.google.common.base.Objects;
import com.shrcl.engineerprocess.domain.Certification;
import com.shrcl.engineerprocess.domain.CertificationNotification;
import com.shrcl.engineerprocess.domain.CorpWeChatUser;
import com.shrcl.engineerprocess.domain.CorpWeChatUserDAO;
import com.shrcl.engineerprocess.domain.Engineer;
import com.shrcl.engineerprocess.domain.EngineerCertification;
import com.shrcl.engineerprocess.domain.EngineerCostEstimation;
import com.shrcl.engineerprocess.domain.EngineerNotifyReceiver;
import com.shrcl.engineerprocess.domain.EngineerProjectInitNotification;
import com.shrcl.engineerprocess.domain.EngineerTraining;
import com.shrcl.engineerprocess.domain.Notification;
import com.shrcl.engineerprocess.domain.NotificationCriteria;
import com.shrcl.engineerprocess.domain.NotificationStatus;
import com.shrcl.engineerprocess.domain.TrainingNotification;
import com.shrct.businessprocess.project.RCProject;
import edu.fudan.mylang.pf.IObjectFactory;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.IteratorUtils;
import org.apache.log4j.Logger;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import wechat.rcv.controller.ArrayWapper;
import wechat.rcv.controller.WXNotification;

@RestController
@Transactional
@ResponseBody
@RequestMapping("/mynotification")
@SuppressWarnings("all")
public class GetMyNotification {
  private final static Logger logger = Logger.getLogger(GetMyNotification.class.getName());
  
  @Autowired
  private IObjectFactory entities;
  
  @Autowired
  private CorpWeChatUserDAO corpWeChatUserDAO;
  
  @RequestMapping(method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
  public ArrayWapper process(final String wechatid, final Integer page, final Integer pagesize) {
    Object _xblockexpression = null;
    {
      GetMyNotification.logger.info("=========>GetMyNotification.processs<============");
      GetMyNotification.logger.info(("=========>GetMyNotification.wechatid<============" + wechatid));
      GetMyNotification.logger.info(("=========>GetMyNotification.page<============" + page));
      GetMyNotification.logger.info(("=========>GetMyNotification.pagesize<============" + pagesize));
      Object _xtrycatchfinallyexpression = null;
      try {
        CorpWeChatUser _findWeChatUser = this.corpWeChatUserDAO.findWeChatUser(wechatid);
        Engineer _engineer = null;
        if (_findWeChatUser!=null) {
          _engineer=_findWeChatUser.getEngineer();
        }
        final EngineerNotifyReceiver receiver = _engineer.asNotifyReceiver();
        NotificationCriteria _createQueryCriteria = this.entities.<Notification, NotificationCriteria>createQueryCriteria(NotificationCriteria.class);
        final Procedure1<NotificationCriteria> _function = new Procedure1<NotificationCriteria>() {
          @Override
          public void apply(final NotificationCriteria it) {
            it.setReceiver(receiver);
            it.setStart(Integer.valueOf((((page).intValue() - 1) * (pagesize).intValue())));
            it.setLimit(pagesize);
          }
        };
        final NotificationCriteria c = ObjectExtensions.<NotificationCriteria>operator_doubleArrow(_createQueryCriteria, _function);
        final ArrayWapper ret = new ArrayWapper();
        Collection<Notification> _list = c.list();
        final Function1<Notification, WXNotification> _function_1 = new Function1<Notification, WXNotification>() {
          @Override
          public WXNotification apply(final Notification n) {
            WXNotification _xblockexpression = null;
            {
              final String notiType = ((Notification) n).getNotificationType();
              WXNotification _wXNotification = new WXNotification();
              final Procedure1<WXNotification> _function = new Procedure1<WXNotification>() {
                @Override
                public void apply(final WXNotification it) {
                  Long _id = n.getId();
                  it.id = _id;
                  SimpleDateFormat _simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  Date _notifyDate = n.getNotifyDate();
                  String _format = _simpleDateFormat.format(_notifyDate);
                  it.date = _format;
                  Date _notifyDate_1 = n.getNotifyDate();
                  int _day = _notifyDate_1.getDay();
                  it.getWeekDay(Integer.valueOf(_day));
                  NotificationStatus _status = n.getStatus();
                  boolean _equals = Objects.equal(_status, NotificationStatus.confirmed);
                  if (_equals) {
                    it.isFinished = Boolean.valueOf(true);
                  } else {
                    it.isFinished = Boolean.valueOf(false);
                  }
                  InputOutput.<String>println(("it.id====>>>>" + it.id));
                  InputOutput.<String>println(("it.title====>>>>" + it.title));
                  InputOutput.<String>println(("it.date====>>>>" + it.date));
                  NotificationStatus _status_1 = n.getStatus();
                  String _plus = ("NotificationStatus====>>>>" + _status_1);
                  InputOutput.<String>println(_plus);
                  boolean _equals_1 = Objects.equal(notiType, "TrainingNotification");
                  if (_equals_1) {
                    final EngineerTraining nd = ((TrainingNotification) n).getEngineerTraining();
                    String _name = nd.getName();
                    it.title = _name;
                  } else {
                    boolean _equals_2 = Objects.equal(notiType, "EngineerProjectInitNotification");
                    if (_equals_2) {
                      final EngineerCostEstimation nd_1 = ((EngineerProjectInitNotification) n).getEngineerCostEstimation();
                      RCProject _project = nd_1.getProject();
                      String _name_1 = _project.getName();
                      it.title = _name_1;
                    } else {
                      boolean _equals_3 = Objects.equal(notiType, "CertificationNotification");
                      if (_equals_3) {
                        final EngineerCertification nd_2 = ((CertificationNotification) n).getEngineerCertification();
                        Certification _certification = nd_2.getCertification();
                        String _name_2 = _certification.getName();
                        it.title = _name_2;
                      }
                    }
                  }
                }
              };
              _xblockexpression = ObjectExtensions.<WXNotification>operator_doubleArrow(_wXNotification, _function);
            }
            return _xblockexpression;
          }
        };
        final Iterable<WXNotification> result = IterableExtensions.<Notification, WXNotification>map(_list, _function_1);
        Iterator<WXNotification> _iterator = result.iterator();
        List _list_1 = IteratorUtils.toList(_iterator);
        ret.result = _list_1;
        int _size = ret.result.size();
        ret.count = Integer.valueOf(_size);
        InputOutput.<String>println(("*******GetMyNotification.result.size******" + ret.count));
        return ret;
      } catch (final Throwable _t) {
        if (_t instanceof Exception) {
          final Exception e = (Exception)_t;
          Object _xblockexpression_1 = null;
          {
            e.printStackTrace();
            _xblockexpression_1 = null;
          }
          _xtrycatchfinallyexpression = _xblockexpression_1;
        } else {
          throw Exceptions.sneakyThrow(_t);
        }
      }
      _xblockexpression = _xtrycatchfinallyexpression;
    }
    return ((ArrayWapper)_xblockexpression);
  }
}
